package com.nineclock.system.controller;

import cn.hutool.json.JSON;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.model.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.model.dto.SysCompanyUserDTO;
import com.nineclock.model.dto.SysDepartmentDTO;
import com.nineclock.model.pojo.SysCompanyUser;
import com.nineclock.system.OrganizationControllerApi;
import com.nineclock.system.excel.ExcelMember;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    SysCompanyUserService sysCompanyUserService;

    @Autowired
    SysDepartmentService sysDepartmentService;

    /**
     * 查询组织所有员工
     *
     * @return
     */
    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> sysCompanyUserDTOS = sysCompanyUserService.queryCompanyMemberList();
        return Result.success(sysCompanyUserDTOS);
    }

    @Autowired
    private SysDepartmentService departmentService;

    /**
     * 查询组织部门列表
     *
     * @return
     */
    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> sysDepartmentDTOS = departmentService.queryDepartmentList();
        return Result.success(sysDepartmentDTOS);
    }

    /**
     * 分页查询部门下的员工数据列表
     * @param page
     * @param pageSize
     * @param keyword
     * @param departmentId
     * @return
     */

    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "departmentId", required = false) Long departmentId) {
        PageResult<SysCompanyUserDTO> pageResult = sysCompanyUserService.members(page, pageSize, keyword, departmentId);
        return Result.success(pageResult);
    }

    /**
     * execl导入企业员工
     * @param excelFile
     * @return
     * @throws IOException
     */
    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        sysCompanyUserService.importData(excelFile);
        return Result.success();
    }

    /**
     * 添加成员信息
     * @param sysDepartmentDTO
     * @return
     */
    @Override
    @PostMapping("/department")
    public Result addDepartment(@RequestBody  SysDepartmentDTO sysDepartmentDTO) {
        departmentService.addDepartment(sysDepartmentDTO);
        return Result.success();

    }

    @PostMapping("/members/list")
    public Result uploadDepartmentMysql(@RequestBody JSON xlsxJson) {
        sysCompanyUserService.uploadDepartmentMysql(xlsxJson);
        return Result.success();
    }

    /**
     * 查询顶级部门的id
     * @return
     */
    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {


        List<Long> depIds = departmentService.queryDepartmentIds();

        return Result.success(depIds);

    }

    /**
     * 根据部门ID, 查询该部门下的员工数量
     * @param departmentIds
     * @return
     */
    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentId(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = departmentService.queryUserCountByDepartmentId(departmentIds);
        return Result.success(count);

    }

    /**
     * 根据部门ID查询子部门及员工列表
     * @param departmentId
     * @param includeMember
     * @return
     */
    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimleDepartment(@RequestParam(value = "id", required = false) Long departmentId,@RequestParam(value = "includeMember" ,required = false) Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> simpleDTOList = sysDepartmentService.querySimleDepartment(departmentId, includeMember);
        return Result.success(simpleDTOList);

    }
}
